package byggeTegning.geometry;

import javax.media.j3d.*;
import javax.vecmath.*;

public class Cube extends Shape3D
{
  private static final int vertexFormat
          = GeometryArray.COORDINATES
          | GeometryArray.NORMALS
          | GeometryArray.TEXTURE_COORDINATE_2;
  
  private static final float width=1.0f;
  private static final float height=1.0f;
  
  // Point3f p=new Point3f(0.0f,0.0f,0.0f);
  private static final float px=0.0f;
  private static final float py=0.0f;
  private static final float pz=0.0f;

  // Point3f q=new Point3f(0.0f,0.0f,1.0f);
  private static final float qx=0.0f;
  private static final float qy=0.0f;
  private static final float qz=1.0f;
    
    // Back Rectangle
  private static final Point3f p0=new Point3f(px,py,pz);
  private static final Point3f p1=new Point3f(px+width,py,pz);
  private static final Point3f p2=new Point3f(px+width,py+height,pz);
  private static final Point3f p3=new Point3f(px,py+height,pz);
    // Front Rectangle
  private static final Point3f q0=new Point3f(qx,qy,qz);
  private static final Point3f q1=new Point3f(qx+width,qy,qz);
  private static final Point3f q2=new Point3f(qx+width,qy+height,qz);
  private static final Point3f q3=new Point3f(qx,qy+height,qz);

  private static final Point2f texCoord[] // SKAL RETTES OPP SENERE
          = { new Point2f(0.0f, 0.0f),
              new Point2f(0.5f, 0.0f),
              new Point2f(0.5f, 0.5f),
              new Point2f(0.0f, 0.5f),
            };

  private static final Point3f[] verts // Forside: punktene nevnes 'mot klokka'
          = { q0, q1, q2, q3,  // front face
              p3, q3, q2, p2,  // top face
              q0, q3, p3, p0,  // left, side face
              p1, p2, q2, q1,  // right, side face
              p1, q1, q0, p0,  // botum face
              p0, p3, p2, p1   // back face
            };
            
            
  // ******************************************************************
  // *** CONSTRUCTOR: Cube
  // ******************************************************************
  public Cube()
  { int i,face;
    QuadArray quad=new QuadArray(24,vertexFormat);
	  quad.setCoordinates(0,verts);

    for(i=0;i<24;i++) quad.setTextureCoordinate(i,texCoord[i%4]);

	  Vector3f normal=new Vector3f();
	  Vector3f v1=new Vector3f();
	  Vector3f v2=new Vector3f();

    Point3f[] pts=new Point3f[4];
    for(i=0;i<4;i++) pts[i] = new Point3f();
    for(face=0;face<6;face++)
    { quad.getCoordinates(face*4,pts);
      v1.sub(pts[1],pts[0]);
      v2.sub(pts[2],pts[0]);
      normal.cross(v1,v2);
      normal.normalize();
      for(i=0;i<4;i++) quad.setNormal((face*4 + i),normal);
    }
	  this.setGeometry(quad);
	  this.setAppearance(new Appearance());
  }
}